package model;

public class Orientation 
{
	private static int ZAXIS = 0;
	private static int XAXIS = 1;
	private static int YAXIS = 2;
	
	//If the orientation change exceeds this margin, it is ignored
	private static float MARGIN = 10;             
	
	private float[] _lastAxis;
	
	public Orientation(float[] axis)
	{
		_lastAxis = axis;
	}
	
	public void setOrientation(float[] axis)
	{
		_lastAxis = axis;
	}
	
	/**
	 * compare the current orientation with the last known orientation
	 * @param newAxis The new orientation to check
	 * @return true if orientation change exceeds defined margin
	 */
	public boolean checkOrientation(float[] newAxis)
	{
		return 
				checkAxis(_lastAxis[ZAXIS], newAxis[ZAXIS]) && 
				checkAxis(_lastAxis[XAXIS], newAxis[XAXIS]) && 
				checkAxis(_lastAxis[YAXIS], newAxis[YAXIS]);
	}
	
	/**
	 * Checks if the changes between two axis values exceed the defined margin.
	 * @param lastValue The last axis value
	 * @param newValue The new axis value
	 * @return true if orientation change exceeds defined margin
	 */
	private boolean checkAxis(float lastValue, float newValue)
	{
		return Math.abs(lastValue - newValue) <= MARGIN;
	}
}

